package com.basin.common.rabbitmq.config;

import com.basin.common.rabbitmq.property.RabbitMqConnectProp;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: Sam ZHONG | sammy1997@aliyun.com
 */
@Configuration
@EnableConfigurationProperties(value = {RabbitMqConnectProp.class})
public class RabbitConfig {

    @Resource
    private RabbitMqConnectProp rabbitMqConnectProp;

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.rabbitMqConnectProp.getHost());
        connectionFactory.setUsername(this.rabbitMqConnectProp.getUsername());
        connectionFactory.setPassword(this.rabbitMqConnectProp.getPassword());
        return connectionFactory;
    }

    @Bean
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }
}
